************************************************************************************
*Replication Code (STATA 16) 
*
*Fueling the populist divide: nativist and cosmopolitan preferences for representation at the elite and mass level
*
*Rosie Campbell and Oliver Heath 
*
*Political Behavior
*
*************************************************************************************

* Data files

* Data contains harmonized data on voters from the BES Wave 6 and candidates from the RAB

use "Campbell Heath replication data.dta"

set scheme plottig


* Table 1 as per text

* Figure 1 as per text

* Table 2: 	PCA on Representation Items, Voters and Candidates

factor rep_local rep_wclass rep_female rep_disability rep_young rep_bame rep_christian rep_lgbt rep_muslim preferMPDegreeW6 if candidate==0, pcf 

factor rep_local rep_wclass rep_female rep_disability rep_young rep_bame rep_christian rep_lgbt rep_muslim rep_pension if candidate==1, pcf

* Table 3	Model fit statistics

gsem (local4 wclass4 female4 disab4 young4 bame4 lgbt4 muslim4 <-, ologit), nocapslatent lclass(C 2) startvalues(randomid, draws(15) seed(123321)) em(iter(5)) nodvheader nonrtolerance
estimates store twoclass
gsem (local4 wclass4 female4 disab4 young4 bame4 lgbt4 muslim4 <-, ologit), nocapslatent lclass(C 3) startvalues(randomid, draws(15) seed(123321)) em(iter(5)) nodvheader nonrtolerance
estimates store threeclass
gsem (local4 wclass4 female4 disab4 young4 bame4 lgbt4 muslim4 <-, ologit), nocapslatent lclass(C 4) startvalues(randomid, draws(15) seed(123321)) em(iter(5)) nodvheader nonrtolerance
estimates store fourclass
gsem (local4 wclass4 female4 disab4 young4 bame4 lgbt4 muslim4 <-, ologit), nocapslatent lclass(C 5) startvalues(randomid, draws(15) seed(123321)) em(iter(5)) nodvheader nonrtolerance
estimates store fiveclass
gsem (local4 wclass4 female4 disab4 young4 bame4 lgbt4 muslim4 <-, ologit), nocapslatent lclass(C 6) startvalues(randomid, draws(15) seed(123321)) em(iter(5)) nodvheader nonrtolerance
estimates store sixclass

estimates stats twoclass threeclass fourclass fiveclass sixclass

* Table 4 	Conditional probabilities, LCA on Representation Items

gsem (local4 wclass4 female4 disab4 young4 bame4 lgbt4 muslim4 <-, ologit), nocapslatent lclass(C 3) startvalues(randomid, draws(15) seed(123321)) em(iter(5)) nodvheader nonrtolerance
estat lcprob
estat lcmean

gsem (local4 wclass4 female4 disab4 young4 bame4 lgbt4 muslim4 <-, ologit), nocapslatent lclass(C 3) startvalues(randomid, draws(15) seed(123321)) em(iter(5)) nodvheader nonrtolerance

predict cpost*, classposteriorpr
egen max = rowmax(cpost*)

generate predclass2 = 1 if cpost1==max
replace predclass2 = 2 if cpost2==max
replace predclass2 = 3 if cpost3==max
replace predclass2 = . if cpost1==.


* Figure 2	Support for cosmopolitan and nativist representation among voters and candidates

mlogit predclass2 i.candidate

margins candidate, predict(outcome(1)) noesample
marginsplot, recast (bar) ytitle ("") xtitle ("") title ("Nativist")
graph save 1.gph, replace 

margins candidate, predict(outcome(2)) noesample
marginsplot, recast (bar) ytitle ("") xtitle ("") title ("Status Quo")
graph save 2.gph, replace 

margins candidate, predict(outcome(3)) noesample
marginsplot, recast (bar) ytitle ("") xtitle ("") title ("Cosmopolitan")
graph save 3.gph, replace 

graph combine 1.gph 2.gph 3.gph, ycommon col (3) 


* Figure 3	Support for increased representation of different social groups, AME

oprobit rep_female i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
estimates store A

oprobit rep_wclass i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
estimates store B

oprobit rep_young i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
estimates store C

oprobit rep_bame i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
estimates store D

oprobit rep_christian i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
estimates store E

oprobit rep_muslim i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
estimates store F

oprobit rep_disability i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
estimates store G

oprobit rep_local i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
estimates store H

coefplot A, bylabel(Women MPs) || B, bylabel(Working class MPs) || C, bylabel(Young MPs) || D, bylabel(BAME MPs) || E, bylabel(Christian MPs) || F, bylabel(Muslim MPs) || G, bylabel(Disabled MPs) || H, bylabel(Local MPs) ||, drop(_cons) xline(0) byopts(compact cols(2)) ylabel(,labsize(vsmall))


* Figure 4	Support for cosmopolitan and nativist representation, AME

mlogit predclass2 i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
margins, dydx(*) post

coefplot (, keep(*:1._predict) label(Nativist)) (, keep(*:2._predict) label(Status Quo)) (, keep(*:3._predict) label(Cosmopolitan)), swapnames xline(0) legend(rows(1)) legend(position(6))

* Figure 5	Support for cosmopolitan vs nativist representation by party affiliation, AME

logit cosmonat i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if pid10==2 [pweight=cweight]
margins, dydx(*) post
estimates store A

logit cosmonat i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if pid10==3 [pweight=cweight]
margins, dydx(*) post
estimates store B

logit cosmonat i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if pid10==4 [pweight=cweight]
margins, dydx(*) post
estimates store C

logit cosmonat i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if pid10==5 [pweight=cweight]
margins, dydx(*) post
estimates store D

logit cosmonat i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if pid10==6 [pweight=cweight]
margins, dydx(*) post
estimates store E

logit cosmonat i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if pid10==7 [pweight=cweight]
margins, dydx(*) post
estimates store F

coefplot A, bylabel(Conservative) || B, bylabel(Labour) || C, bylabel(Libdem) || D, bylabel(UKIP) || E, bylabel(Green) || F, bylabel(Nationalists) ||, drop(_cons) xline(0)

* Figure 6	Support for anti-elite attitudes, AME

oprobit efficacyPolCareW6 i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 polAttentionW6 ib9.pid8 ib2.predclass2 lr_scale al_scale
estimates store G
coefplot G, drop(_cons) xline(0)

* Figure 7	Support for Brexit, AME

logit brexit i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 polAttentionW6 ib9.pid8 ib2.predclass2 lr_scale al_scale efficacyPolCareW6 
estimates store H
coefplot H, drop(_cons) xline(0)


*************************************************************************************


*** Appendix

* Table A1: Preferences for representation by social background of candidates and voters, weighted

oprobit rep_female i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
outreg2 using "TableA1", word excel dec(2) append

oprobit rep_wclass i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
outreg2 using "TableA1", word excel dec(2) append

oprobit rep_young i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
outreg2 using "TableA1", word excel dec(2) append

oprobit rep_bame i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
outreg2 using "TableA1", word excel dec(2) append

oprobit rep_christian i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
outreg2 using "TableA1", word excel dec(2) append

oprobit rep_muslim i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight]
outreg2 using "TableA1", word excel dec(2) append

mlogit predclass2 i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate [pweight=cweight], base(2) 
outreg2 using "TableA1", word excel dec(2) append

* Table A2: Preferences for representation by social background of candidates and voters, unweighted

oprobit rep_female i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate
outreg2 using "TableA2", word excel dec(2) append

oprobit rep_wclass i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate
outreg2 using "TableA2", word excel dec(2) append

oprobit rep_young i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate
outreg2 using "TableA2", word excel dec(2) append

oprobit rep_bame i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate
outreg2 using "TableA2", word excel dec(2) append

oprobit rep_christian i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate
outreg2 using "TableA2", word excel dec(2) append

oprobit rep_muslim i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate
outreg2 using "TableA2", word excel dec(2) append

mlogit predclass2 i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate, base(2) 
outreg2 using "TableA2", word excel dec(2) append

* Table A3: Preferences for representation by social background of voters

oprobit rep_female i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==0
outreg2 using "TableA3", word excel dec(2) append

oprobit rep_wclass i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==0
outreg2 using "TableA3", word excel dec(2) append

oprobit rep_young i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==0
outreg2 using "TableA3", word excel dec(2) append

oprobit rep_bame i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==0
outreg2 using "TableA3", word excel dec(2) append

oprobit rep_christian i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==0
outreg2 using "TableA3", word excel dec(2) append

oprobit rep_muslim i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==0
outreg2 using "TableA3", word excel dec(2) append

mlogit predclass2 i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==0, base(2) 
outreg2 using "TableA3", word excel dec(2) append

* Table A4: Preferences for representation by social background of candidates

oprobit rep_female i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==1
outreg2 using "TableA4", word excel dec(2) append

oprobit rep_wclass i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==1
outreg2 using "TableA4", word excel dec(2) append

oprobit rep_young i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==1
outreg2 using "TableA4", word excel dec(2) append

oprobit rep_bame i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==1
outreg2 using "TableA4", word excel dec(2) append

oprobit rep_christian i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==1
outreg2 using "TableA4", word excel dec(2) append

oprobit rep_muslim i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==1
outreg2 using "TableA4", word excel dec(2) append

mlogit predclass2 i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4  if candidate==1, base(2) 
outreg2 using "TableA4", word excel dec(2) append


* Table A5: Support for populist attitudes and Brexit

logit brexit i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 polAttentionW6 ib9.pid8 ib2.predclass2 lr_scale al_scale efficacyPolCareW6
outreg2 using "TableA5", word excel dec(2) append

oprobit efficacyPolCareW6 i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 polAttentionW6 ib9.pid8 ib2.predclass2 lr_scale al_scale
outreg2 using "TableA5", word excel dec(2) append

* Figure A1	Support for increased representation of different social groups among voters, AME

oprobit rep_female i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==0
estimates store A

oprobit rep_wclass i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==0
estimates store B

oprobit rep_young i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==0
estimates store C

oprobit rep_bame i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==0
estimates store D

oprobit rep_christian i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==0
estimates store E

oprobit rep_muslim i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==0
estimates store F

oprobit rep_disability i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if candidate==0
estimates store G

oprobit rep_local i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if candidate==0
estimates store H

coefplot A, bylabel(Women MPs) || B, bylabel(Working class MPs) || C, bylabel(Young MPs) || D, bylabel(BAME MPs) || E, bylabel(Christian MPs) || F, bylabel(Muslim MPs) || G, bylabel(Disabled MPs) || H, bylabel(Local MPs) ||, drop(_cons) xline(0) byopts(compact cols(2)) ylabel(,labsize(vsmall))




* Figure A2	Support for increased representation of different social groups among candidates, AME

oprobit rep_female i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==1
estimates store G

oprobit rep_wclass i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==1
estimates store H

oprobit rep_young i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==1
estimates store I

oprobit rep_bame i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==1
estimates store J

oprobit rep_christian i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==1
estimates store K

oprobit rep_muslim i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 if candidate==1
estimates store L

oprobit rep_disability i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if candidate==1
estimates store M

oprobit rep_local i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 i.candidate if candidate==1
estimates store N

coefplot G, bylabel(Women MPs) || H, bylabel(Working class MPs) || I, bylabel(Young MPs) || J, bylabel(BAME MPs) || K, bylabel(Christian MPs) || L, bylabel(Muslim MPs) || M, bylabel(Disabled MPs) || N, bylabel(Local MPs) ||, drop(_cons) xline(0) byopts(compact cols(2)) ylabel(,labsize(vsmall))


* Figure A3	Support for Brexit (selected covariates), AME

logit brexit i.gender i.age3a i.bame5 i.class_id i.religion2 i.edlevel4 polAttentionW6 ib9.pid8 ib2.predclass2 lr_scale al_scale efficacyPolCareW6 immig
estimates store H
coefplot H, keep(*.predclass2 lr_scale al_scale immig efficacyPolCareW6 polAttentionW6) xline(0)
